package com.wjk.kylin.id.core.factory;

import com.wjk.kylin.id.core.generator.IdGenerator;

import java.util.concurrent.ConcurrentHashMap;

/**
 *
 */
public abstract class AbstractIdGeneratorFactory implements IdGeneratorFactory {
    private static final ConcurrentHashMap<String, IdGenerator> GENERATORS = new ConcurrentHashMap<>();

    public IdGenerator getIdGenerator(String bizType) {
        if (GENERATORS.containsKey(bizType)) {
            return GENERATORS.get(bizType);
        } else {
            synchronized (this) {
                if (GENERATORS.containsKey(bizType)) {
                    return GENERATORS.get(bizType);
                } else {
                    IdGenerator idGenerator = this.createIdGenerator(bizType);
                    GENERATORS.put(bizType, idGenerator);
                    return idGenerator;
                }
            }
        }
    }

    public abstract IdGenerator createIdGenerator(String bizType);

}
